package com.yjian.controller;

import com.yjian.common.SDataResult;
import com.yjian.controller.bean.CheckUserExistRequest;
import com.yjian.controller.bean.RegisterRequest;
import com.yjian.controller.bean.SendSmsRequest;
import com.yjian.service.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * Created by 许小宝 on 2017/9/27.
 */
@Log4j
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "店铺登录", notes = "手机号+密码登录")
    @PostMapping("/v1/login")
    public Object checkUserExist(@Valid @RequestBody CheckUserExistRequest request,
                                 HttpServletRequest httpServletRequest) {
        try {
            return userService.checkUserAndPassword(request.getPhone(), request.getPassword());
        } catch (Exception e) {
            //
            System.out.println(e.getMessage());
            log.info("登录异常:{}"+e.getMessage());
        }
        return SDataResult.fail();
    }

    @ApiOperation(value = "店铺注册", notes = "店铺注册")
    @PostMapping("/v1/register")
    public Object register(@Valid @RequestBody RegisterRequest request,
                           HttpServletRequest httpServletRequest) {
        try {
            return userService.register(request.getPhone(),request.getCode(),request.getPassword(),request.getConfirmPassword());
        } catch (Exception e) {
            //
            log.info("店铺注册:{}"+e.getMessage());
        }
        return SDataResult.fail();
    }

    @ApiOperation(value = "用户发送验证码", notes = "用户发送验证码")
    @PostMapping("/v1/sms")
    public Object sendSms(@Valid @RequestBody SendSmsRequest request,
                          HttpServletRequest httpServletRequest) {
        try {

            return null;
        } catch (Exception e) {
            //
            log.info("用户发送验证码:{}"+e.getMessage());
        }
        return SDataResult.fail();
    }

    @GetMapping("/redirect")
    public Object redirectUrl(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {

        httpServletResponse.setHeader("content-type", "text/html;charset=utf-8");
        StringBuilder sb = new StringBuilder();
        sb.append("<html>\n")
                .append("<head>\n")
                .append("<script type=\"text/javascript\">\n")
                .append("window.location.href='").append("http://www.baidu.com").append("'")
                .append("</script>\n")
                .append("</head>\n")
                .append("<body>\n")
                .append("\n")
                .append("</body>\n")
                .append("</html>");
        return sb.toString();
    }
}
